﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LabManager.UserControls.Tabs.Templates;
using DataConnector;
using System.Data.Entity;
using Equin.ApplicationFramework;
using Library;

namespace LabManager.UserControls.Tabs.Products
{
    public partial class TabProducts : TabTemplate
    {
        private BindingListView<V_Product> bindingList;
        private BindingSource bindingSource = new BindingSource();
        private int selectedID = -1;

        public TabProducts()
        {
            InitializeComponent();
        }

        public override void Load()
        {
            SetTexts();
            LoadProductTypes();
            LoadPackageInserts();
            LoadProductList();
        }

        private void SetTexts()
        {
            //TODO
        }

        private void LoadProductTypes()
        {
            var productType = new BindingListView<ProductType>(DataConnectorLayer.Instance.R_ProductType.FindAll(x => x.ProductTypeID > 0).ToList<ProductType>());

            cbProductTypes.DataSource = productType;
            cbProductTypes.ValueMember = "ProductTypeID";
            cbProductTypes.DisplayMember = "ProductDescription";
        }

        private void LoadPackageInserts()
        {
            var packageInsert = new BindingListView<PackageInsert>(DataConnectorLayer.Instance.R_PackageInsert.FindAll(x => x.PackageInsertID > 0).ToList<PackageInsert>());

            cbPackageInserts.DataSource = packageInsert;
            cbPackageInserts.ValueMember = "PackageInsertID";
            cbPackageInserts.DisplayMember = "Description";   
        }

        private void LoadProductList()
        {
            // Get new entities from view
            bindingList = new BindingListView<V_Product>(DataConnectorLayer.Instance.R_V_Product.FindAll(x => x.ProductID > 0).ToList<V_Product>());
            bindingSource.DataSource = bindingList;
            dgMain.DataSource = bindingSource;
            bnNavi.BindingSource = bindingSource;

            if (dgMain.Rows.Count > 0)
            {
                selectedID = (int)dgMain.Rows[0].Cells[0].Value;
            }

            dgMain_SelectionChanged(null, null);
        }

        #region Events

        private void dgMain_SelectionChanged(object sender, EventArgs e)
        {
            if (dgMain.SelectedRows.Count > 0)
            {
                var selectedRow = dgMain.SelectedRows[0];

                selectedID = (int)selectedRow.Cells[0].Value;
                cbProductTypes.Text = selectedRow.Cells[1].Value.ToString();
                cbPackageInserts.Text = selectedRow.Cells[2].Value.ToString();
                tbOrderNumber.Text = selectedRow.Cells[3].Value.ToString();
                tbLot.Text = selectedRow.Cells[4].Value.ToString();
                dtTagDate.Text = selectedRow.Cells[5].Value.ToString();
                dtExpirationDate.Value = DateTime.Parse(selectedRow.Cells[6].Value.ToString());

                // Delete button
                var canDeleteProductObject = DataConnectorLayer.Instance.R_ProductObject.FindOne(x => x.ProductID == selectedID) == null;

                btDelete.Enabled = canDeleteProductObject;

                // Update
                btUpdate.Enabled = true;
            }
            else
            {
                tbOrderNumber.Text = string.Empty;
                tbLot.Text = string.Empty;
                dtTagDate.Value = DateTime.Now;
                dtExpirationDate.Value = DateTime.Now;

                btUpdate.Enabled = false;
                btDelete.Enabled = false;
            }

            // New button
            var canCreateProductType = DataConnectorLayer.Instance.R_ProductType.Set<ProductType>().Count() > 0;
            var canCreatePackageInsert = DataConnectorLayer.Instance.R_PackageInsert.Set<PackageInsert>().Count() > 0;
            btNew.Enabled = canCreateProductType && canCreatePackageInsert;
        }

        private void btNew_Click(object sender, EventArgs e)
        {
            var productTypeID = DataConnectorLayer.Instance.R_ProductType.FindOne().ProductTypeID;
            var packageInsertID = DataConnectorLayer.Instance.R_PackageInsert.FindOne().PackageInsertID;

            var item = new Product() { ProductTypeID = productTypeID, PackageInsertID = packageInsertID, OrderNumber = Helper.EMPTY_STRING, LOT = Helper.EMPTY_STRING, TagDate = DateTime.Now, ExpirationDate = DateTime.Now };
            DataConnectorLayer.Instance.R_Product.Create(item);
            DataConnectorLayer.Instance.R_Product.SaveChanges();

            LoadProductList();
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            var item = DataConnectorLayer.Instance.R_Product.FindById(selectedID);
            DataConnectorLayer.Instance.R_Product.Delete(item);
            DataConnectorLayer.Instance.R_Product.SaveChanges();

            LoadProductList();
        }

        private void btUpdate_Click(object sender, EventArgs e)
        {
            var item = DataConnectorLayer.Instance.R_Product.FindById(selectedID);

            item.ProductTypeID = (int)cbProductTypes.SelectedValue;
            item.PackageInsertID = (int)cbPackageInserts.SelectedValue;
            item.OrderNumber = tbOrderNumber.Text;
            item.LOT = tbLot.Text;
            item.TagDate = dtTagDate.Value;
            item.ExpirationDate = dtExpirationDate.Value;

            DataConnectorLayer.Instance.R_Product.Update(item);
            DataConnectorLayer.Instance.R_Product.SaveChanges();

            LoadProductList();
        }

        private void tbFilter_TextChanged(object sender, EventArgs e)
        {
            var search = tbFilter.Text.ToLower();

            bindingList.ApplyFilter(delegate(V_Product product)
            {
                return  product.ProductDescription.ToString().ToLower().Contains(search) ||
                        product.Description.ToString().ToLower().Contains(search) ||
                        product.OrderNumber.ToLower().Contains(search) ||
                        product.LOT.ToLower().Contains(search) ||
                        product.TagDate.ToString().ToLower().Contains(search) ||
                        product.ExpirationDate.ToString().ToLower().Contains(search);
            });
        }

        #endregion

    }
}